package com.rfid.project.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.rfid.project.model.Authority;
import com.rfid.project.model.People;

/**
 * Servlet Filter implementation class UserFilter
 */
public class UserFilter extends HttpServlet implements Filter {
       
    /**
	 * 
	 */
	private static final long serialVersionUID = -795795532822838684L;

	/**
     * @see HttpServlet#HttpServlet()
     */
    public UserFilter() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String url = request.getRequestURI();
		if ((url.length() == 1 && (url.equals("/")))
				|| url.startsWith("/css/") 
				|| url.startsWith("/images/") 
				|| url.startsWith("/js/") 
				|| url.startsWith("/login")) {
			chain.doFilter(request, response);
			return;
		}
		
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("currentUser");
		if (null == obj) {
			response.sendRedirect(request.getContextPath());
            return;
		}
		chain.doFilter(request, response);
		
		
		/*
		String url = request.getRequestURI();
		if ((url.length() == 1 && (url.equals("/")))
				|| url.startsWith("/css/") 
				|| url.startsWith("/images/") 
				|| url.startsWith("/js/") 
				|| url.startsWith("/login")) {
			chain.doFilter(request, response);
			return;
		}
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("currentUser");
		Object powers = session.getAttribute("powers");
		if (null == obj || null == powers) {
			response.sendRedirect(request.getContextPath());
            return;
		}
		People currentUser = (People) obj;
		if (currentUser.getId() == 0) {
			chain.doFilter(request, response);
			return;
		}
		
		List<Authority> authoritys = (List<Authority>) powers;
		url = url.substring(1);
		for (Authority auth : authoritys) {
			if (url.equals(auth.getCode())) {
				chain.doFilter(request, response);
				return;
			}
		}
		response.sendRedirect(request.getContextPath());
		//chain.doFilter(request, response);
		 */
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
